<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends CommonController {
    public function index()
    {
        $title = '注册 - Fly社区';
        $this->assign('title',$title);
        $question = D('vercode')->getOne();
        $this->assign('question',$question);
        $this->display();
    }

    public function checkdata()
    {
        if ($_POST) {
            $this->checkVercode();

            if ($_POST['email'] != '') {
                if (!preg_match('/^[a-z0-9][a-z0-9_]{2,}@([a-z0-9]+|[a-z0-9][a-z0-9-]+[a-z0-9])([\.][a-z0-9]+)+$/', $_POST['email'])) {
                    $arr = array('error'=>'email不合要求');
                    echo json_encode($arr);
                    exit;
                } else {
                    if (M('users')->where('email="'.$_POST['email'].'"')->find()) {
                        $arr = array('error'=>'email已存在');
                        echo json_encode($arr);
                        exit;
                    }
                }
            } else {
                $arr = array('error'=>'email不能为空');
                echo json_encode($arr);
                exit;
            }

            if ($_POST['nickname'] != '') {
                if (!preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}]{1}[a-zA-Z0-9_\x{4e00}-\x{9fa5}]+$/u', $_POST['nickname'])) {
                    $arr = array('error'=>$_POST['nickname'].'不合要求');
                    echo json_encode($arr);
                    exit;
                }
            } else {
                $arr = array('error'=>'昵称不能为空');
                echo json_encode($arr);
                exit;
            }

            if ($_POST['pass'] != '') {
                if (!preg_match('/^[\S]{6,12}$/', $_POST['pass'])) {
                    $arr = array('error'=>'密码长度不合要求');
                    echo json_encode($arr);
                    exit;
                }
            } else {
                $arr = array('error'=>'密码不能为空');
                echo json_encode($arr);
                exit;
            }

            $data = array('uname' => $_POST['nickname'],
                          'upwd' => md5($_POST['pass']),
                          'email' => $_POST['email'],
                          'create_time' => time(),
                          'login_time' => time(),
                          'uext' => array('avatar' => 'Public/avatar/'.mt_rand(0,9).'.jpg',
                                          'token' => md5($_POST['nickname'].$_POST['email'].time())
                                               )
                          );
            
            $this->reg($data);

            unset($_SESSION['answer']);//清除验证答案

            echo json_encode(['info'=>'注册成功']);
        } else {
            $this->error('非法访问','/home/index/index',1);
        }
    }

    public function reg($data){
        $User = D("Users");
        $userid = $User->relation(true)->add($data);
        $_SESSION['nickname'] = $_POST['nickname'];
        $_SESSION['uid'] = $userid;
        $_SESSION['email'] = $_POST['email'];
        $_SESSION['avatar'] = $data['uext']['avatar'];
    }

    public function QQReg($data)
    {
        $uinfo['create_for_qq'] = 1;//标记通过QQ登录创建帐号

        $uinfo['uname'] = $data['nickname'];//记录QQ昵称

        $uinfo['upwd'] = md5(getRandStr(12));//创建随机密码

        $uinfo['create_time'] = time();//创建注册时间

        $uinfo['login_time'] = time();//创建最后登录时间

        $uinfo['sex'] = $data['gender'];//获取性别
        
        $uinfo['uext'] = array('avatar' => getQQFace($data['figureurl_qq_2']),
                              'qq' => $data['qq'],
                              'token' => md5($data['nickname'].$_POST['email'].time())
                              );

        if (!$data['city'] == '') {
            $uinfo['city'] = $data['city'];
        }

        $res = D('users')->relation(true)->add($uinfo);

        setcookie("nickname", $uinfo['uname'], time()+31536000, '/');

        $_SESSION['nickname'] = $uinfo['uname'];

        $_SESSION['avatar']=$uinfo['uext']['avatar'];

        return $res;
    }
}